﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using StudentManagement.Extensions.Pagenation;
using StudentManagement.Infrastructure.Repositories;
using StudentManagement.Models;
using StudentManagement.Services;
using System;

namespace StudentManagement.Controllers
{
    [Authorize]
    public class CourseController : Controller
    {
        private readonly IRepository<Course> courseRepository;
        private readonly ICourseService courseService;

        public CourseController(IRepository<Course> courseRepository, ICourseService courseService)
        {
            this.courseRepository = courseRepository;
            this.courseService = courseService;
        }
        
        public IActionResult Index(PageParameters pageParameters)
        {
            ViewBag.PageTitle = "课程列表";
            ViewBag.SortBy = pageParameters.SortBy.Trim();
            ViewBag.SearchString = pageParameters.SearchString?.Trim();

            var model = courseService.GetPagedData(pageParameters);

            return View(model);
        }

        public IActionResult Details(int id)
        {
            var course = courseRepository.Get(id);
            if (course != null) {
                ViewBag.PageTitle = "课程明细";
                return View(course);
            }
            else {
                Response.StatusCode = 404;
                return View("error");
            }
        }

        [HttpGet]
        public IActionResult Find(int id)
        {
            var course = courseRepository.Get(id);
            return new JsonResult(course);
        }

        [HttpGet]
        public IActionResult Create()
        {
            ViewBag.PageTitle = "新增课程";
            return View();
        }

        [HttpPost]
        public IActionResult Create(Course course)
        {
            if (ModelState.IsValid) {
                courseRepository.Insert(course);
                return RedirectToAction("Index");
            }

            ViewBag.PageTitle = "添加课程";
            return View();
        }

        [HttpPost]
        public IActionResult Add(Course course)
        {
            if (ModelState.IsValid) {
                courseRepository.Insert(course);                  
            }
            return RedirectToAction("Index");
        }

        [HttpGet]
        public IActionResult Edit(int id)
        {
            var student = courseRepository.Get(id);
            ViewBag.PageTitle = "编辑课程";
            return View(student);
        }

        [HttpPost]
        public IActionResult Edit(Course course)
        {
            if (ModelState.IsValid) {
                courseRepository.Update(course);
                return RedirectToAction("Index");
            }
            return View();
        }

        [HttpPost]
        public IActionResult Update(Course course)
        {
            if (ModelState.IsValid) {
                courseRepository.Update(course);
            }
            return RedirectToAction("Index");
        }

        [HttpGet]
        public IActionResult Delete(int id)
        {
            var student = courseRepository.Get(id);
            if (student != null) {
                courseRepository.Delete(student);
                return RedirectToAction("Index");
            }

            return View();
        }

        [HttpPost]
        public IActionResult Remove(int id)
        {
            var student = courseRepository.Get(id);
            if (student != null) {
                courseRepository.Delete(student);
            }

            return RedirectToAction("Index");
        }
    }
}
